<?php

namespace App\Store\Controllers\Basic;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Basic\GoodsCraft;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Validate\Basic\GoodsCraft as ValidateBasicGoodsCraft;
use App\Store\Services\Excel\DownloadTemp;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 商品工艺信息
 */
class GoodsCraftController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = GoodsCraft::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsCraft::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['craft_name', 'remark']);

        $Validate = new ValidateBasicGoodsCraft();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $isexist = GoodsCraft::where([['craft_name', $req_data['craft_name']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '工艺名称不允许重复']);
        }
        $data = $req_data;
        $data['create_account_id'] = $accountinfo['id'];
        $data['update_account_id'] = $accountinfo['id'];
        $data['create_account_real_name'] = $accountinfo['real_name'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['cp_id'] = $accountinfo['cp_id'];
        $data['create_at'] = time();
        $data['update_at'] = time();
        GoodsCraft::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'craft_name', 'remark']);

        $Validate = new ValidateBasicGoodsCraft('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = GoodsCraft::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $isexist = GoodsCraft::where([['id', '<>', $req_data['id']], ['craft_name', $req_data['craft_name']], ['cp_id', $accountinfo['cp_id']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '工艺名称不允许重复']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        GoodsCraft::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsCraft::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        GoodsCraft::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function dodownload(Request $request)
    {
        $row = [['工艺名称', '备注']];
        $options = [];
        $datawith = [
            'A' => '25',
            'B' => '50',
        ];
        $filename = '商品工艺信息导入模板' . time() . '.xlsx';
        return Excel::download(new DownloadTemp($row, $options, $datawith), $filename);
    }

    /**
     * 上传导入，先调用uploadfile,返回地址调用该接口
     */
    public function doimport(Request $request)
    {

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $filepathname = $request->filepathname;
        if ($filepathname != '' && file_exists(storage_path() . $filepathname) && is_file(storage_path() . $filepathname)) {
            $file = storage_path() . $filepathname;
            $data = Excel::toArray(null, $file)[0];
            $ins_data = [];
            foreach ($data as $key => $val) {
                if ($key == 0) { //表头跳过
                    if(count($val)!=2){
                        return response()->json(['code' => '001', 'msg' =>'导入数据格式错误']); 
                    }
                    continue;
                }
                $valdata = [
                    'cp_id' => $cp_id,
                    'craft_name' => $val[0],
                    'remark' => $val[1] ? $val[1] : '',
                    'create_account_id' => $accountinfo['id'],
                    'update_account_id' => $accountinfo['id'],
                    'create_account_real_name' => $accountinfo['real_name'],
                    'update_account_real_name' => $accountinfo['real_name'],
                    'create_at' => time(),
                    'update_at' => time(),
                ];
                $isexist = GoodsCraft::where([['craft_name', $val[0]], ['cp_id', $cp_id]])->first();
                if ($isexist) {
                    continue;
                }
                $item = $valdata;
                $ins_data[] = $item;
            }
            GoodsCraft::insert($ins_data);
            return response()->json(['code' => '000', 'msg' => '数据导入成功']);
        } else {
            return response()->json(['code' => '001', 'msg' => '文件不存在'], 200);
        }
    }
}
